package cn.th.service.impl;

import cn.th.entity.Dormitory;
import cn.th.entity.School;
import cn.th.mapper.SchoolMapper;
import cn.th.service.ISchoolService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 学校 服务实现类
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {

    @Autowired
    private ISchoolService schoolService;

    @Autowired
    private SchoolMapper schoolMapper;

    /**
     * 查询学校
     * @param page
     * @param pageSize
     * @param school
     * @return
     */
    @Override
    public Map<String, Object> querySchool(int page, int pageSize, String school) {
        Page<School> page1 = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<School> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(school),School::getName,school);

        //添加排序条件,根据id升序排
        wrapper.orderByAsc(School::getId);

        //执行查询
        schoolService.page(page1,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("rows",page1.getRecords());

        return data;
    }

    /**
     * 新增学校，采用excel表格形式
     * @param file
     * @return
     */
    @Transactional
    public ResponseEntity<String> saveFile(MultipartFile file) {
        try {
            // 读取Excel文件
            InputStream inputStream = file.getInputStream();
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历Excel表格数据,从第二行开始读
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                Row row = sheet.getRow(i);

                String school = row.getCell(0).getStringCellValue(); // 获取每行的第一个单元格中的内容

                // 进行操作，对new出来的School进行赋值
                School school1 = new School();
                school1.setName(school);

                // 将数据存入数据库
                schoolService.save(school1);
            }
            return ResponseEntity.ok("Excel上传成功");
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel上传失败");
        }
    }

    /**
     * 删除学校，可批量删除
     * @param ids
     */
    @Transactional
    public void removeBatchByIds(String ids) {
        schoolMapper.removeBatchByIds(ids);
    }
}
